package com.gitee.wsl.compose.modifier.animate

import androidx.compose.animation.core.CubicBezierEasing
import androidx.compose.animation.core.TweenSpec
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.focusable
import androidx.compose.foundation.gestures.awaitFirstDown
import androidx.compose.foundation.gestures.waitForUpOrCancellation
import androidx.compose.foundation.hoverable
import androidx.compose.foundation.interaction.FocusInteraction
import androidx.compose.foundation.interaction.Interaction
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.PressInteraction
import androidx.compose.foundation.interaction.collectIsFocusedAsState
import androidx.compose.foundation.interaction.collectIsHoveredAsState
import androidx.compose.material3.ColorScheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.surfaceColorAtElevation
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.draw.scale
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.graphics.drawscope.scale
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Dp

@Composable
fun Modifier.animatedScale(
    scale: Float,
    interactionSource: MutableInteractionSource
): Modifier {
    val interaction by interactionSource.interactions.collectAsState(
        initial = FocusInteraction.Focus()
    )

    val animationSpec = defaultScaleAnimationSpec(interaction)

    val animatedScale by animateFloatAsState(
        targetValue = scale,
        animationSpec = animationSpec
    )

    return drawWithContent {
        scale(animatedScale) {
            this@drawWithContent.drawContent()
        }
    }
}

private fun defaultScaleAnimationSpec(interaction: Interaction): TweenSpec<Float> = tween(
    durationMillis = when (interaction) {
        is FocusInteraction.Focus -> 300
        is FocusInteraction.Unfocus -> 500
        is PressInteraction.Press -> 120
        is PressInteraction.Release -> 300
        is PressInteraction.Cancel -> 300
        else -> 300
    },
    easing = CubicBezierEasing(0f, 0f, 0.2f, 1f)
)